﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mukhopatro.BusinessLogicLayer;
using Mukhopatro.BusinessEntities;
using Imgalib.Utility;

namespace Mukhopatro.Admin
{
    public partial class edit_news : System.Web.UI.Page
    {
        private Int32 ContentId
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.QueryString != null && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
                    {
                        return Convert.ToInt32(Cryptography.Decrypt(HttpContext.Current.Request.QueryString["id"]));
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch
                {
                    return -1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }

        }

        private void LoadData()
        {
            CustomerNewsContentBLL bll = new CustomerNewsContentBLL();
            CustomerNewsContent news= bll.GetCustomerNewsContentById(ContentId);
            if (news != null)
            {
                txtTite.Text = news.Title;
                CKEditor1.Text = news.NewsContent;
                chkShowNews.Checked = news.IsActive;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CustomerNewsContentBLL bll = new CustomerNewsContentBLL();
            CustomerNewsContent news = bll.GetCustomerNewsContentById(ContentId);
            if (news != null)
            {
                news.Title = txtTite.Text.Trim();
                news.NewsContent = CKEditor1.Text.Trim();
                news.IsActive = chkShowNews.Checked;
                try
                {
                    bll.UpdateCustomerNewsContent(news);
                    respMessage.SetSuccessMessage("News Update Successful.");
                }
                catch(Exception ex)
                {
                    respMessage.SetSuccessMessage("News Update Failed." + ex.Message);
                }
            }
            else
            {
                respMessage.SetFailMessage("No News To Update With Specified Id.");
            }
        }
    }
}